<?php
			
	/* SVN FILE: $Id: select.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: michele.andreoletti@gmail.com $
	* @version $Revision: 8 $
	* @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	* @filesource $URL: http://arcadia.googlecode.com/svn/trunk/sysop/select.php $
	*/

	require_once '../include/functions.php';
	require_once '../include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';
	
	fnOnly4Sysop('','json');
	
	$sScript = fnCurrentScript();
	
	fnGetValueFromGET();
	
	$sOutput = $sQuery = '';
	
	switch ($sAction) {
	
		case "users_groups_lnk":
			switch ($sType) {
				case "user":
					$aData = fnRemainingGroups($sUsername);
					$sWhat = 'sGroup';
					break;
				case "group":
					$aData = fnRemainingUsers($sUsername);
					$sWhat = 'sUser';
					break;
			}
			
			$sOutput .= fnSelectJsonOutput($aData, $sWhat);
			break;
										
		default:
			$sOutput = fnJsonOutput($sScript, $sAction, '', 101);
			break;
	
	}	

	echo $sOutput;

?>


<?php

	function fnSelectJsonOutput ($aData, $sWhat) {
		
		$sOutput = '';
		
		if (!empty($aData)) {
			$sOutput .= "[ ";
			$sOutput .= "{ \"sItem\": \"\", \"sDisplayname\": \"Seleziona...\" }, ";
			
			foreach ($aData as $aRow) {
				$sOutput .= "{ \"sItem\": \"".$aRow[$sWhat]."\", \"sDisplayname\": \"".$aRow['sDisplayname']."\" }, ";
			
			}
			
			$sOutput = substr($sOutput, 0, -2)." ]";
		
		} else {
			$sOutput .= "[ { \"sItem\": \"\", \"sDisplayname\": \"Nessuna opzione disponibile\" } ]";
		
		} 
		
		return 	$sOutput;
	}
	
	function fnRemainingGroups ($sUsername) {	
		
		$aData = array();
		
		// Tutti i gruppi
		$aAllGroups = fnGetDataFromQuery(fnQueryGetAllGroups());
		
		// Gruppi in cui è inserito $sUsername
		$aAssociatedGroups = fnGetDataFromQuery(fnQueryGetAssociatedGroups($sUsername));
		
		// Inserisco in $aData i gruppi presenti in $aAllGroups ma non presenti in $aAssociatedGroups
		foreach ($aAllGroups as $aGroup) {
			$bAssociated = false;
			
			foreach ($aAssociatedGroups as $aAssociatedGroup) {
				if ($aGroup['sGroup'] == $aAssociatedGroup['sGroup']) {
					$bAssociated = true;
				}
			}
			
			if (!$bAssociated) {
				$aData[] = $aGroup;
			}
		
		}
		
		return $aData;
	}
	
	function fnRemainingUsers ($sGroup) {	
		
		$aData = array();
		
		// Tutti gli utenti
		$aAllUsers = fnGetDataFromQuery(fnQueryGetAllUsers());
		
		// Utenti che fanno parte di $sGroup
		$aMembersUsers = fnGetDataFromQuery(fnQueryGetMembersUsers($sGroup));
		
		// Inserisco in $aData i gruppi presenti in $aAllGroups ma non presenti in $aAssociatedGroups
		foreach ($aAllUsers as $aUser) {
			$bAssociated = false;
			
			foreach ($aMembersUsers as $aMembersUser) {
				if ($aUser['sUser'] == $aMembersUser['sUser']) {
					$bAssociated = true;
				}
			}
			
			if (!$bAssociated) {
				$aData[] = $aUser;
			}
		
		}
		
		return $aData;
	}

?>
